package scau.zzf.base.common;

import scau.zzf.dictionary.Code;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义响应结构
 * Created by zzf on 2017/4/11.
 */
public class Result implements Serializable {
    // 响应业务状态
    private Integer code;

    // 响应消息
    private String msg;

    // 响应中的数据
    private Map data;

    private Result(String name, Object data) {
        this.data = new HashMap<>();
        this.data.put(name, data);
        code = Code.OPERATION_SUCCESS.getCode();
        msg = Code.OPERATION_SUCCESS.getMessage();
    }

    private Result(String name, Object data, long total) {
        this.data = new HashMap<>();
        this.data.put(name, data);
        this.data.put("total", total);
         code = Code.OPERATION_SUCCESS.getCode();
        msg = Code.OPERATION_SUCCESS.getMessage();
    }

    private Result(IBaseCode code) {
        this.code = code.getCode();
        msg = code.getMessage();

    }

    public Result(String message) {
        code=0;
        msg = message;
    }


    public static Result ok(String name,Object data) {
        return new Result(name,data);
    }

    public static Result list(String name, Object data, long total) {
        return new Result(name,data,total);
    }

//    public static Result ok(Object data) {
//        return new Result(data);
//    }

//    public static Result build(String name, Object data) {
//
//    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map getData() {
        return data;
    }

    public void setData(HashMap data) {
        this.data = data;
    }

    public static Result Build(IBaseCode code) {
        return new Result(code);
    }

    public static Result ok() {
        return new Result(Code.OPERATION_SUCCESS);
    }

    public static Result fail() {
        return new Result(Code.OPERATION_Fail);
    }

    public static Result fail(String message) {
        return new Result(message);
    }
}
